package za.co.felixsol.sf.dxf.reader;

import java.io.Serializable;

/**
 * Simple entry on Dxf program stream - comprise code and value
 * @author norman
 *
 */
public class DxfEntry implements Serializable {
	
	private static final long serialVersionUID = 1L;

	private String code;
	private String value;

	public DxfEntry() {
		super();
	}

	public DxfEntry(String code, String value) {
		this();
		if (code != null) {
			code = code.trim();
		}
		this.code = code;
		this.value = value;
	}

	public Integer getCode() {
		if (code != null) {
			return Integer.parseInt(code.trim());
		}
		return null;
	}

	public void setCode(String code) {
		if (code != null) {
			code = code.trim();
		}
		this.code = code;
	}

	// ToDo - Tidy up conversions fopro when we do binnary reads
	private String getString(String value) {
		return value;
	}

	private Double getDouble(String value) {
		if (value == null) return null;
		value = value.trim();
		return Double.parseDouble(value);
	}

	private Integer getInteger(String value) {
		if (value == null) return null;
		value = value.trim();
		return Integer.parseInt(value);
	}

	private Boolean getBoolean(String value) {
		if (value == null) return null;
		value = value.trim();
		return Boolean.parseBoolean(value);
	}

	@SuppressWarnings("unchecked")
	public <T> T getValue() {
		if ((getCode() >= 0) && (getCode() <= 9)) return (T)getString(value);
		if ((getCode() >= 10) && (getCode() <= 39)) return (T)getDouble(value);
		if ((getCode() >= 40) && (getCode() <= 59)) return (T)getDouble(value);
		if ((getCode() >= 60) && (getCode() <= 79)) return (T)getInteger(value);
		if ((getCode() >= 90) && (getCode() <= 99)) return (T)getInteger(value);
		if (getCode() == 100) return (T)getString(value);
		if (getCode() == 102) return (T)getString(value);
		if (getCode() == 105) return (T)getString(value); // ToDo Hex
		if ((getCode() >= 110) && (getCode() <= 149)) return (T)getDouble(value);
		if ((getCode() >= 160) && (getCode() <= 169)) return (T)getInteger(value);
		if ((getCode() >= 170) && (getCode() <= 179)) return (T)getInteger(value);
		if ((getCode() >= 210) && (getCode() <= 239)) return (T)getDouble(value);
		if ((getCode() >= 270) && (getCode() <= 279)) return (T)getInteger(value);
		if ((getCode() >= 280) && (getCode() <= 289)) return (T)getInteger(value);
		if ((getCode() >= 290) && (getCode() <= 299)) return (T)getBoolean(value);
		if ((getCode() >= 300) && (getCode() <= 309)) return (T)getString(value);
		if ((getCode() >= 310) && (getCode() <= 319)) return (T)getString(value); // ToDo Hex
		if ((getCode() >= 320) && (getCode() <= 329)) return (T)getString(value); // ToDo Hex
		if ((getCode() >= 330) && (getCode() <= 369)) return (T)getString(value); // ToDo Hex
		if ((getCode() >= 370) && (getCode() <= 379)) return (T)getInteger(value);
		if ((getCode() >= 380) && (getCode() <= 389)) return (T)getInteger(value);
		if ((getCode() >= 390) && (getCode() <= 399)) return (T)getString(value); // ToDo Hex
		if ((getCode() >= 400) && (getCode() <= 409)) return (T)getInteger(value);
		if ((getCode() >= 410) && (getCode() <= 419)) return (T)getString(value);
		if ((getCode() >= 420) && (getCode() <= 429)) return (T)getInteger(value);
		if ((getCode() >= 430) && (getCode() <= 439)) return (T)getString(value);
		if ((getCode() >= 440) && (getCode() <= 449)) return (T)getInteger(value);
		if ((getCode() >= 450) && (getCode() <= 459)) return (T)getInteger(value);
		if ((getCode() >= 460) && (getCode() <= 469)) return (T)getDouble(value);
		if ((getCode() >= 470) && (getCode() <= 479)) return (T)getString(value);
		if ((getCode() >= 480) && (getCode() <= 481)) return (T)getString(value); // ToDo Hex
		if (getCode() == 999) return (T)getString(value);
		if ((getCode() >= 1000) && (getCode() <= 1009)) return (T)getString(value);
		return (T)value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	@Override
	public String toString() {
		return this.code + "=" + this.value;
	}
	
}
